<?php

use App\Common\DbSelector;
use App\Process\MonitorProcess;
use Swoft\Crontab\Process\CrontabProcess;
use Swoft\Db\Pool;
use Swoft\Http\Server\HttpServer;
use Swoft\Task\Swoole\SyncTaskListener;
use Swoft\Task\Swoole\TaskListener;
use Swoft\Task\Swoole\FinishListener;
use Swoft\Rpc\Client\Client as ServiceClient;
use Swoft\Rpc\Client\Pool as ServicePool;
use Swoft\Rpc\Server\ServiceServer;
use Swoft\Http\Server\Swoole\RequestListener;
use Swoft\WebSocket\Server\WebSocketServer;
use Swoft\Server\SwooleEvent;
use Swoft\Db\Database;
use Swoft\Redis\RedisDb;
use App\Http\Middleware\LoginMiddleware;

return [
    'httpServer'        => [
        'class'    => HttpServer::class,
        'port'     => config('application.HTTP_PORT',8004),
        'listener' => [
//            'tcp' => bean('tcpServer'),
//             'ws' => bean('wsServer'),
//             'rpc' => bean('rpcServer')
        ],
        'process'  => [
//            'monitor' => bean(MonitorProcess::class)
//            'crontab' => bean(CrontabProcess::class)
        ],
        'on'       => [
//            SwooleEvent::TASK   => bean(SyncTaskListener::class),  // Enable sync task
            SwooleEvent::TASK   => bean(TaskListener::class),  // Enable task must task and finish event
            SwooleEvent::FINISH => bean(FinishListener::class)
        ],
        /* @see HttpServer::$setting */
        'setting' => [
            // enable static handle
            'enable_static_handler'    => true,
            // swoole v4.4.0以下版本, 此处必须为绝对路径
            'document_root' => dirname(__DIR__),
            'task_worker_num'       => 12,
            'task_enable_coroutine' => true,
            'worker_num'            => 6,
            'package_max_length'    => 20971520,  //大约20M
            //支持https,签名文件
//            'ssl_cert_file' => '/my/certs/2288803_www.domain.com.pem',
//            'ssl_key_file'  => '/my/certs/2288803_www.domain.com.key',
        ]
    ],
    'httpDispatcher'    => [
        // Add global http middleware
        'middlewares'      => [
            // \Swoft\Whoops\WhoopsMiddleware::class,
            \Swoft\View\Middleware\ViewMiddleware::class,

        ],
        'afterMiddlewares' => [
            \Swoft\Http\Server\Middleware\ValidatorMiddleware::class
        ]
    ],
    'db' => [
        'class'    => Database::class,
        'prefix'   => 'chat_',
        'charset' => 'utf8mb4',
        'config'   => [
            'collation' => 'utf8mb4_unicode_ci',
            'strict'    => true,
            'timezone'  => '+8:00',
            'modes'     => 'NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES',
            'fetchMode' => PDO::FETCH_ASSOC,
        ],
        'writes' => [
            [
                'dsn'      => "mysql:dbname=im;host=172.25.0.21:8066",
                'username' => 'root',
                'password' => 'DRsXT5ZJ6Oi55LPQ'
            ]
        ],
        'reads'  => [
            [
                'dsn'      => "mysql:dbname=im;host=172.25.0.21:8066",
                'username' => 'root',
                'password' => 'DRsXT5ZJ6Oi55LPQ'
            ]
        ]
    ],
    'db.pool' => [
        'class'    => Pool::class,
        'database' => bean('db'),
    ],
    'migrationManager'  => [
        'migrationPath' => '@app/Migration',
    ],
    'redis'             => [
        'class'    => RedisDb::class,
        'host'     => config('redis.HOST'),
        'port'     => config('redis.PORT',6379),
        'database' => 0,
        'option'   => [
            'prefix' => 'service-im:'
        ]
    ],
    'wsServer'          => [
        'class'   => WebSocketServer::class,
        'port'    => config('application.WS_PORT',8005),
        'on'      => [
            // Enable http handle
            SwooleEvent::REQUEST => bean(RequestListener::class),
            SwooleEvent::TASK   => bean(TaskListener::class),  // Enable task must task and finish event
            SwooleEvent::FINISH => bean(FinishListener::class)
        ],
        'debug'   => 1,
        // 'debug'   => env('SWOFT_DEBUG', 0),
        /* @see WebSocketServer::$setting */
        'setting' => [
            'task_worker_num'       => 12,
            'task_enable_coroutine' => true,
            'log_file' => alias('@runtime/swoole.log'),
        ],
    ],
    'lineFormatter'      => [
        'format'     => '%datetime% [%level_name%] [%event%]  %messages%',
        'dateFormat' => 'Y-m-d H:i:s',
    ],
    'noticeHandler'      => [
        'class'     => \Swoft\Log\Handler\FileHandler::class,
        'logFile'   => '@runtime/logs/notice.log',
        'formatter' => \bean('lineFormatter'),
//        'levels'    => 'notice,info,debug,trace',
        'levels'    => 'info,notice',
    ],
    'applicationHandler' => [
        'class'     => \Swoft\Log\Handler\FileHandler::class,
        'logFile'   => '@runtime/logs/error.log',
        'formatter' => \bean('lineFormatter'),
        'levels'    => 'error,warning',
    ],
    'logger'             => [
        'flushRequest' => false,
        'enable'       => true,
        'handlers'     => [
            'application' => \bean('applicationHandler'),
            'notice'      => \bean('noticeHandler'),
        ],
    ]
];
